package com.lrm.web;

import com.lrm.biz.IBlogBiz;
import com.lrm.biz.ITagBiz;
import com.lrm.biz.ITypeBiz;
import com.lrm.pojo.Blog;
import com.lrm.pojo.Tag;
import com.lrm.pojo.Type;
import com.lrm.util.Result;
import com.lrm.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/12/5.
 * @Modified By:
 */
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private IBlogBiz blogBiz;

    @Autowired
    private ITagBiz tagBiz;

    @Autowired
    private ITypeBiz typeBiz;

    @GetMapping("blogList")
    public Result blogList(@PageableDefault(size = 8, sort = {"updataTime"}, direction = Sort.Direction.DESC)
                                       Pageable pageable) {
        Page<Blog> blogs = blogBiz.listBlog(pageable);
        return ResultUtil.success(blogs);
    }
    @GetMapping("/tagList/{size}")
    public Result tagList(@PathVariable Integer size){
        List<Tag> tags = tagBiz.listTag(size);
        return ResultUtil.success(tags);
    }
    @GetMapping("/typeList/{size}")
    public Result typeList(@PathVariable Integer size){
        List<Type> types = typeBiz.listType(size);
        return ResultUtil.success(types);
    }

    /**
     * 推荐查询人数多的
     * @param pageable
     * @return
     */
    @GetMapping("blogrecommendList")
    public Result blogrecommendList(@PageableDefault(size = 8, sort = {"views"}, direction = Sort.Direction.DESC)
                                   Pageable pageable) {
        List<Blog> blogs = blogBiz.blogrecommendList(pageable);
        return ResultUtil.success(blogs);
    }


    @PostMapping("blogList")
    public Result blogSenrchList(@PageableDefault(size = 8, sort = {"updataTime"}, direction = Sort.Direction.DESC)
                                   Pageable pageable,@RequestParam String senrch) {
        Page<Blog> blogs = blogBiz.blogSenrchList(senrch,pageable);
        return ResultUtil.success(blogs);
    }

    @GetMapping("/getBlog/{id}")
    public Result getBlog(@PathVariable Long id){
        Blog blog = blogBiz.findByIdmk(id);
        return ResultUtil.success(blog);
    }
}
